package com.uyii.springbootredis.service;

import com.uyii.springbootredis.model.Article;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ArticleService {

    @Autowired
    private ArticleRepository ArticleRepository;

    public Article createArticle(Article article) {
        return ArticleRepository.save(article);
    }

    public Optional<Article> getArticleById(String id) {
        return ArticleRepository.findById(id);
    }

    public Iterable<Article> getAllPeople() {
        return ArticleRepository.findAll();
    }

    public Article updateArticle(String id, Article updatedArticle) {
        if (ArticleRepository.existsById(id)) {
            updatedArticle.setId(id);
            return ArticleRepository.save(updatedArticle);
        } else {
            return null;
        }
    }

    public void deleteArticle(String id) {
        ArticleRepository.deleteById(id);
    }
}